<?php

/**
 * Description of ContentAdminGenres
 *
 * @author Honza, Tung
 */
class ContentAdminGenres implements IMainBodyWithInteraction {

  private $showTable = true;	//show data which is selected from DB
  private $showForm = true;		//show form for add or edit data
  private $error;
  
  public function action() {
	//delete a row base on genre ID
    if ($_GET['a'] == 'erase') {
      DB::sql('delete from ' . TBL_GENRES . ' where id_genre = ' . $_GET['ida']);
    }
	
	//show details of genre
    if ($_GET['a'] == 'detail') {
      $this->showTable = false;
      $result = DB::sql('select * from ' . TBL_GENRES . ' where id_genre = ' . $_GET['ida']);
      $detail .= '<p>' . $result[0]['name'] . '</p>';
      $detail .= '<p>' . $result[0]['description'] . '</p>';
      return $detail;
    }
  }

  public function applyForm($formID = NULL) {
	//add a new genre
    if (isset($_POST['genreSubmit'])) {
      //check input data to make sure that there is not special character in query
      $genreName = addslashes($_POST['genreName']);
      $genreDesc = addslashes($_POST['genreDesc']);
	  //show an error message if $genreName or $genreDesc is empty
      if (!$genreName || !$genreDesc) {
        $this->error = '<p class="error">All fields are compulsory</p>';
      } else {
		//turn off apply Form and add new location information
        $this->showForm = false;
        DB::sql('insert into ' . TBL_GENRES . " values (0, '$genreName', '$genreDesc')");
      }
    }

	//edit data base on genre ID
    if (isset($_POST['genreEdit'])) {
      //$showTable = false;
	  $genreName = addslashes($_POST['genreName']);
      $genreDesc = addslashes($_POST['genreDesc']);
	  //show an error message if $genreName or $genreDesc is empty
      if (!$genreName || !$genreDesc) {
        $this->error = '<p class="error">All fields are compulsory</p>';
      } else {
      DB::sql('update ' . TBL_GENRES . ' set name = \'' . $_POST['genreName'] . '\', description = \'' . $_POST['genreDesc'] . '\' where id_genre = ' . $_POST['ida']);
      $this->showForm = false;
	  }
    }
  }

  //getContent function is called automatically 
  public function getContent() {
    $content = '<h2>Genres</h2>';
    $content .= $this->action();
    $this->applyForm();

    $form = $this->getForm();
    if ($this->showTable) {
      $content .= '<a href="index.php?where=admin&subwhere=genres&a=new">Create genre</a>';
    }
    $content .= $form;
    $content .= $this->getTable();

    return $content;
  }

  //create form for add a new genre or edit a genre
  public function getForm($formID = NULL, $error = NULL) {
    if ($_GET['a'] == 'new' && $this->showForm) {
      $this->showTable = false;

      $f = new Form();
      $f->startForm('self', 'post', NULL, array("class" => "form_settings"));
      $f->addLabel('Name', 'genreName')->addInput('text', 'genreName', $_POST['genreName']);
      $f->addLabel('Description', 'genreDesc')->addInput('text', 'genreDesc', $_POST['genreDesc']);
      $f->addLabel('&nbsp', 'genreSubmit')->addInput('submit', 'genreSubmit', 'Create genre', array("class" => "submit"));
      $f->endForm();
      
      return $this->error . $f->getForm();
    }


    if ($_GET['a'] == 'edit' && $this->showForm) {
      $this->showTable = false;
      
      $result = DB::sql('select * from ' . TBL_GENRES . ' where id_genre = ' . $_GET['ida']);
      $f = new Form();
      $f->startForm('self', 'post', NULL, array("class" => "form_settings"));
      $f->addLabel('Name', 'genreName')->addInput('text', 'genreName', $result[0]['name']);
      $f->addLabel('Description', 'genreDesc')->addInput('text', 'genreDesc', $result[0]['description']);
      $f->addLabel('&nbsp', 'genreEdit')->addInput('submit', 'genreEdit', 'Edit genre', array("class" => "submit"));
      $f->addInput('hidden', 'ida', $_GET['ida']);
      $f->endForm();
      
      return $this->error . $f->getForm();
    }
  }

  public function getSubmitedForm() {
    
  }

  //create table to show data
  public function getTable($tableID = NULL) {
    if ($this->showTable) {
      //table of all genres
      $result = DB::sql('select * from ' . TBL_GENRES . ' order by upper(name)');

      $table = '<table>';
      $table .= '<tr>';
      $table .= '<th>Name</th><th>Detail</th><th>Edit</th><th>Erase</th>';
      $table .= '</tr>';
      foreach ($result as &$row) {
        $genreID = $row['id_genre'];
        $table .= '<tr>';
        $table .= '<td>' . $row['name'] . '</td>';
        $table .= '<td><a href="index.php?where=admin&subwhere=genres&a=detail&ida=' . $genreID . '">Detail</a></td>';
        $table .= '<td><a href="index.php?where=admin&subwhere=genres&a=edit&ida=' . $genreID . '">Edit</a></td>';
        $table .= '<td><a href="index.php?where=admin&subwhere=genres&a=erase&ida=' . $genreID . '">Erase</a></td>';
        $table .= '</tr>';
      }
      $table .= '';
      $table .= '</table>';

      return $table;
    }
  }

}

?>
